<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
require_once APP . '/application/models/ArticleModel.php';
require_once APP.'/application/models/Alllist.php';

/**
 * 新闻管理
 * @package admin
 */
class ArticleController extends AdminAction
{
    

    public function init()
    {
    }
    
    
 	/**
     * 创建分类
     *
     * @param unknown_type $type
     * @param unknown_type $default
     * @return unknown
     */
    private function create($type, $defaultid=null, $edit=false)
    {
    	global $cfg;
    	
    	$obj_list = new Alllist($this->db);

    	$result = '';
    	$arr = $obj_list->getAllListByType($type);
    	
    	foreach ($arr as $v){
    		$select = '';
    		
    		// 如果叶子节点已经有关联文章，则忽略
    		if ($v['isleaf'] && !empty($v['leafid'])){
    			$exist = $this->db->fetchOne("select count(*) from article where cid=? and aid=?", array($v['id'], $v['leafid']));
    			if ($exist && ($defaultid != $v['id'] || $edit == false)){
    				$result .= '<optgroup label="' . $v['name'] . '(已有关联)"></optgroup>';
    				continue;
    			}
    		}
    		if ($defaultid == $v['id'])
    			$select = 'selected=true';
    		
    		$result .= '<option value="' . $v['id'] . '" ' . $select . '>' . $v['name'] . '</option>';
    	}
    	
    	return $result;
    }

    /**
     * 加载框架页
     */
    public function addAction()
    {
    	global $cfg;
    	
    	$type = $this->getParam("type");
    	$aid = (int)$this->getParam("aid");
    	$cid = $this->getParam("cid");    	
    	$nowp = (int) $this->getParam("nowp");
    	
    	$title = "添加";
    	$content = array();
    	$edit = false;
    	$recommend = false;
    	$recseq = false;
    	$maxrecseq = 0;
    	
    	if (!empty($aid)){
    		$edit = true;
    		$title = "编辑";
    		$obj_article = new ArticleModel($this->db);
    		$content = $obj_article->show($aid);
    	}
    	
    	if ($type == $cfg['listtype']['xshd']){// 学术活动
    		$recommend = true;
    		$recseq = true;
    		$maxrecseq = $this->db->fetchOne("select max(recseq) from article");
    	}
    	    	
    	$this->view->recseq = $recseq;
    	$this->view->maxrecseq = $maxrecseq;
    	$this->view->showimage= false;
    	$this->view->showdesc= false;
    	$this->view->option = $this->create($type, $cid, $edit);
    	$this->view->recommend = $recommend;
    	
    	$this->view->title = $title;
        $this->view->content = $content;
    	
        $this->view->cid = $cid;
        $this->view->aid = $aid;
        $this->view->nowp = $nowp;
        $this->view->type = $type;
        
        if ($type == $cfg['listtype']['ltrw']){
        	$this->render("ltrw");
        }
    }
    
    
	public function doaddAction()
    {
    	$param = $this->getAllParams();
    	$cid = (int) $param['cid'];
    	$aid = (int) $param['aid'];
    	$type = (int) $param['type'];
    	
    	if (!empty($aid)){
    		$this->doeditAction();
    		return;
    	}
    	
    	if (empty($param['title'])){
    		$url = $this->url($this->view->baseUrl, "article", "add", "cid=" . $cid . "&type=" . $type, "admin");
	    	$this->showmessage($url, '标题不能为空');
	    	return;
    	}
    	
    	// 置顶处理
    	if ($this->getParam("top") == 1){
    		$param['seq'] = $this->db->fetchOne("select max(seq)+1 from article where cid=" . $cid);
    	}
    	
    	$image = Util::upfile("image", "image");
    	!empty($image) && $param['image'] = $image;
    	
    	$pdf = Util::upfile("pdf", "pdf");
    	!empty($pdf) && $param['pdf'] = $pdf;
    	
    	$obj_list = new Alllist($this->db);
    	$arr_list_info = $obj_list->getDetailByID($cid);
    	if ($arr_list_info['isleaf'] && !empty($arr_list_info['leafid'])){
    		$exist = $this->db->fetchOne("select count(*) from article where cid=? and aid=?", array($cid, $arr_list_info['leafid']));
    		if ($exist){
		    	$this->view->msg = '该分类为叶子分类，不能再添加！';
		    	
		    	$this->view->url = $this->url($this->view->baseUrl, "article", "add", "type=" . $type. '&cid=' . $cid, "admin");
		    	$this->showmessage();
		    	exit;
    		}
    	}
    	
    	$obj_article = new ArticleModel($this->db);
    	$flag_success = false;
    	try{
    		$aid = $obj_article->add($param);
    		$this->view->msg = '添加成功';
    		$flag_success = true;
    		if ($arr_list_info['isleaf']){
    			$obj_list->change($cid, array("leafid" => $aid));
    		}
    	}catch (Exception $e){
    		$tihs->view->msg = "添加失败！" . $e->getMessage();
    	}
    	   	
    	$this->view->url = $this->url($this->view->baseUrl, "article", "add", "cid=" . $cid . '&type=' . $type, "admin");
    	$this->showmessage();
    }
    
    
    
    
    /**
     * 编辑
     *
     */
    public function doeditAction()
    {
    	$param = $this->getAllParams();
    	$cid = (int) $param['cid'];
    	$aid = (int) $param['aid'];
    	$type = (int)$param['type'];
    	
    	if (!isset($param['recommend']))
    		$param['recommend'] = 0;
    	if (!isset($param['btnpdf']))
    		$param['btnpdf'] = 0;
    	
    	if (empty($param['title'])){
    		$url = $this->url($this->view->baseUrl, "article", "add", "aid=$aid&cid=" . $cid . '&type=' . $type, "admin");
	    	$this->showmessage($url, '标题不能为空');
	    	return;
    	}
    	
    	// 置顶处理
    	if ($this->getParam("top") == 1){
    		$param['seq'] = $this->db->fetchOne("select max(seq)+1 from article where cid=" . $cid);
    	}
    	
    	$image = Util::upfile("image", "image");
    	!empty($image) && $param['image'] = $image;
    	
    	$pdf = Util::upfile("pdf", "pdf");
    	!empty($pdf) && $param['pdf'] = $pdf;
    	
    	// 删除
    	if (!empty($param['delimage']))
    		$param['image'] = "";
    	
    	if (!empty($param['delpdf']))
    		$param['pdf'] = "";
    	
    	
    	$obj_article = new ArticleModel($this->db);
    	$flag_success = false;
    	try{
    		$aid = $obj_article->change($aid, $param);
    		$msg = '编辑成功';
    		$flag_success = true;
    	}catch (Exception $e){
    		$msg = "编辑失败！" . $e->getMessage();
    	}
    	
    	$content = $obj_article->show($aid);
    	
    	$url = $this->url($this->view->baseUrl, "article", "list", "nowp=" . $param['nowp'] . '&cid=' . $cid . '&type=' . $type, "admin");
    	$this->showmessage($url, $msg);
    }
    
    
    public function listAction()
    {
    	global $cfg;
    	$cid = (int) $this->getParam("cid");
    	$nowp = (int) $this->getParam("nowp", 1);
    	$query = trim($this->getParam("query", ""));
    	$type = (int) $this->getParam("type");
    	
    	$per_page = 20;
    	
    	$obj_list = new Alllist($this->db);
		$arr = $obj_list->getAllListByType($type);
		
		$arr_types = array();
		foreach ($arr as $v){
			$arr_cid[] = $v['id'];
			$arr_types[$v['id']] = $v['name'];
		}
		if (empty($arr_cid))
			$where = 'where 1!=1';
		else
			$where = ' where cid in (' . implode(',', $arr_cid) . ')';
		
		if ($cid != 0)
			$where = " where cid=$cid";
		
		if (!empty($query)){
			$query = mysql_escape_string($query);
			$where .= " and (title like '%$query%' or content like '%$query%' or keywords like '%$query%' or author like'%$query%')";
		}
		
		$url = $this->url($this->view->baseUrl, "article", "list", "type=$type&cid=0");
    	if ($cid != 0)
			$str_nav = "&nbsp;<a href='{$url}'>全部</a>&nbsp;";
		else 
			$str_nav = "全部";
			
    	foreach ($arr_types as $index => $v){
    		$url = $this->url($this->view->baseUrl, "article", "list", "type=$type&cid=" . $index);
    		if ($index == $cid)
    			$str_nav .= $v;
    		else
    			$str_nav .= "&nbsp;<a href='{$url}'>{$v}</a>&nbsp;";
    	}
    	
		$obj_article = new ArticleModel($this->db);
		$result = $obj_article->fetchAll($cid, $nowp, $per_page, $where);
    	
    	$this->view->cid = $cid;
    	$this->view->nowp = $nowp;
        $this->view->list = $result['list'];
        $this->view->page = $result['page'];
        $this->view->pagenum = Util::pageNumStyle($result['page']);
        $this->view->str_nav = $str_nav;
        $this->view->type = $type;
        
        $query = stripslashes($query);        	
        $this->view->query = $query;
        
        if ($type == $cfg['listtype']['ltrw']){
        	$this->render("ltrwlist");
        }
    }
    
    
    /**
     * 删除
     *
     */
    public function delAction()
    {
    	$aid = (int) $this->getParam("aid");
    	$cid = (int)$this->getParam("cid");
    	$type = (int)$this->getParam("type");
    	$nowp = (int) $this->getParam("nowp");
    	$query = $this->getParam("query");
    	
    	$obj_article = new ArticleModel($this->db);
    	$content = $obj_article->show($aid);
    	$obj_article->del($aid);
    	
    	$url = $this->url($this->view->baseUrl, "article", "list", "nowp=" . $nowp . '&cid=' . $cid . '&type=' . $type, "admin");
    	
    	if (!empty($query)){
    		$url .= '&query=' . urlencode($query);
    	}
    	    	
    	header("location:" . $url);
    }
}

function secho($obj, $key, $def="")
{
	if (isset($obj['content']) && isset($obj['content'][$key])){
		if ($key == 'pubtime')
			$obj['content'][$key] = substr($obj['content'][$key], 0, 10);
			
		echo htmlspecialchars($obj['content'][$key]);
	}else{
		if (!empty($def))
			echo $def;
	}
}